package com.woniuxy.myorders.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.woniuxy.commons.annotation.RequirePerms;
import com.woniuxy.commons.entity.Orders;
import com.woniuxy.commons.entity.PageBean;
import com.woniuxy.commons.stafflog.LogRequirePerms;
import com.woniuxy.commons.utils.ResponseResult;
import com.woniuxy.myorders.service.OrdersService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lhc
 * @create 2021-11-12 9:42
 */
@RestController
@RequestMapping("/myorders")
public class OrdersController {
    @Resource
    private OrdersService ordersService;
    /**
     *分页查询
     */
    @SentinelResource(value = "selectPage",fallback ="fallbackSelectPage")
    @GetMapping("/selectPage")
    public ResponseResult<PageBean<Orders>> selectPage(Orders orders,Integer pageSize,Integer pageIndex){
        System.out.println("查询条件:"+orders);
        System.out.println("pageSize:"+pageSize);
        System.out.println("pageIndex:"+pageIndex);
        PageBean<Orders> pageBean=new PageBean<>();
        int countOrdersByPage = ordersService.selectCountOrdersByPage(orders, pageIndex, pageSize);
        List<Orders> orderss = ordersService.selectOrdersByPage(orders, pageIndex, pageSize);
        pageBean.setBeanList(orderss);
        pageBean.setPageIndex(pageIndex);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalRecord(countOrdersByPage);
        return new ResponseResult<PageBean<Orders>>(200,null , "获取成功", pageBean);
    }

    /**
     * 分页查询降级方法
     * @return
     */
    public ResponseResult<PageBean<Orders>> fallbackSelectPage(){
        return new ResponseResult<>(500,null , "系统维护中", null);
    }

    /**
     * 修改订单状态
     * @param orders
     * @return
     */
    @LogRequirePerms(description = "修改订单状态")
    @RequirePerms("user:update")//修改权限
    @SentinelResource(value = "orderStates",fallback ="fallPutOrderState")//开启哨兵
    @PutMapping("/orderState")
    public ResponseResult logPutOrderState(@RequestBody Orders orders){
        System.out.println("orders"+orders);
        ordersService.updateOrdersById(orders);
        return new ResponseResult(200, null, "修改成功", null);
    }

    /**
     * 修改订单状态降级方法
     * @return
     */
    public ResponseResult fallPutOrderState(){
        return new ResponseResult(500, null, "系统维护中", null);
    }
}
